package org.hackystat.devcathlon.ui.page.profile;

import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.team.TeamManager;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.teams.invitations.TeamsInvitationPage;
import org.hackystat.devcathlon.ui.page.teams.profile.TeamsProfile;

/**
 * Team listing panel is used to assist listing all associated teams of a user.
 * @author Anthony Du
 */
public class TeamListingPanel extends Panel {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * Team listings constructor panel.
   * @param id unique panel id
   * @param teams list of teams
   */
  public TeamListingPanel(String id, List<Team> teams) {
    super(id);
    add(new ListView("teams", teams) {
      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(ListItem item) {
        Team team = (Team)item.getModelObject();
        final String teamName = team.getName();
        final String projectName = team.getProjectName();
        Link teamProfileLink = new Link("teamProfileLink") {
          private static final long serialVersionUID = 1L;
          @Override
          public void onClick() {
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(teamName);
            setResponsePage(new TeamsProfile());
          }          
        };
        teamProfileLink.add(new Label("teamName", teamName));
        item.add(teamProfileLink);
        item.add(new Label("projectName", projectName));
      }
    });
    
    // Display alert for team invitation.
    String email = DevcathlonSession.get().getEmail();
    int numOfInvites = TeamManager.getInstance().getTeamInvites(email).size();
    Link teamInviteLink = new Link("InvitationLink") {
      private static final long serialVersionUID = 1L;

      @Override
      public void onClick() {
        DevcathlonSession.get().getTeamsSession().clearSelectedTeam();
        setResponsePage(TeamsInvitationPage.class);
      }      
    };
    
    String inviteMessage = "";
    if (numOfInvites > 0) {
      inviteMessage = "You have " + numOfInvites + " team invites";
    }
    teamInviteLink.add(new Label("InviteMessage", inviteMessage));
    add(teamInviteLink);
  }
}
